<?php
//Header établie la connection à la base $connection
require("../API/php/header.php");

//Fonctions : Fonctions personnelles de l'instance
require("../php/fonctions.php");

//Mode debug
$modeDebug = false;

//Public ou privé (clé obligatoire)
$modePublic = true;

//Mode de sortie text,json,xml,csv
//Pour le text utiliser $strSorti pour la sortie.
//Pour le json $object_retour sera decoder
//Pour le xml et csv $object_retour->data["resultat"]->data doit contenir qu'un est unique array.
$modeSortie = "json";

//Liens de test
// phpsql/mysql_actionDecompte.php?milis=123450&ctrl=ok&id_tournoi=5&action=start

// IN obligatoire
$arrayInput = array(
    "id_tournoi" => null,
    "action" => null
);

//Définition des entrants optionel
$arrayInputOpt = array(
    "option" => null
);

//Récupération des entrants
$arrayValeur = recupInput($arrayInput,$arrayInputOpt);

//Object retour minima
// $object_retour->strErreur string
// $object_retour->data  string
// $object_retour->statut  string

//--------------------------------------------------------------------------
$strSql = "SELECT *
    FROM `".$prefixTable."tab_tournois` a
    WHERE 1=1
    AND a.`id` = :id_tournoi
;";
$req = $connection->prepare($strSql);
$req->bindValue(":id_tournoi", $arrayValeur["id_tournoi"], PDO::PARAM_INT);

$rows = array();
if($req->execute()){
    $rows = $req->fetch();
    $titre = $rows["titre"];
    $dateDebut = $rows["dateDebut"];
    $dateFin = $rows["dateFin"];
    $nbJetonsTapis = $rows["nbJetonsTapis"];
    $valeurTapis = $rows["valeurTapis"];
    $roundEnCours = $rows["roundEnCours"];
    $etatDecompte = $rows["etatDecompte"];
}else{
    die('Erreur SQL:'.print_r($req->errorInfo(), true)." (".$strSql.")");
}
$req->closeCursor();

//--------------------------------------------------------------------------
$strSql = "SELECT *
    FROM `".$prefixTable."tab_rounds` a
    WHERE 1=1
    AND a.`id_tournoi` = :id_tournoi
    AND a.`numRound` = :roundEnCours
;";
$req = $connection->prepare($strSql);
$req->bindValue(":id_tournoi", $arrayValeur["id_tournoi"], PDO::PARAM_INT);
$req->bindValue(":roundEnCours", $roundEnCours, PDO::PARAM_INT);

$rows = array();
if($req->execute()){
    $rows = $req->fetch();
    $temps = $rows["temps"];
    $tempsRestantSeconde = $rows["tempsRestantSeconde"];
    $debutDecompte = $rows["debutDecompte"];
    $small_blind = $rows["small_blind"];
    $big_blind = $rows["big_blind"];
    $ante = $rows["ante"];
}else{
    die('Erreur SQL:'.print_r($req->errorInfo(), true)." (".$strSql.")");
}
$req->closeCursor();

//--------------------------------------------------------------------------
//START
if($arrayValeur["action"] == "start"){
    if($dateDebut == '0000-00-00 00:00:00'){
        $strSql = "UPDATE `".$prefixTable."tab_tournois`
            SET `dateDebut` = NOW()
            WHERE 1=1
            AND `id` = ".$arrayValeur["id_tournoi"]."
        ;";
        $req = $connection->prepare($strSql);

        if(!($req->execute())){
            die('Erreur SQL:'.print_r($req->errorInfo(), true)." (".$strSql.")");
        }
        $req->closeCursor();
    }

    $strSql = "UPDATE `".$prefixTable."tab_tournois`
        SET `etatDecompte` = 'encours'
        WHERE 1=1
        AND `id` = ".$arrayValeur["id_tournoi"]."
    ;
    
    UPDATE `".$prefixTable."tab_rounds`
        SET `debutDecompte` = NOW()
        WHERE 1=1
        AND `id_tournoi` = ".$arrayValeur["id_tournoi"]."
        AND `numRound` = ".$roundEnCours."
    ;";
    $req = $connection->prepare($strSql);
    if(!($req->execute())){
        die('Erreur SQL:'.print_r($req->errorInfo(), true)." (".$strSql.")");
    }
    $req->closeCursor();
} 

//--------------------------------------------------------------------------
//PAUSE
if($arrayValeur["action"] == "pause"){
    $strSql = "UPDATE `".$prefixTable."tab_tournois`
        SET `etatDecompte` = 'pause'
        WHERE 1=1
        AND `id` = ".$arrayValeur["id_tournoi"]."
    ;
    
    UPDATE `".$prefixTable."tab_rounds`
        SET 
        `tempsRestantSeconde` = TIMESTAMPDIFF(SECOND, NOW(), (debutDecompte + INTERVAL temps*60 SECOND))
        WHERE 1=1
        AND `id_tournoi` = ".$arrayValeur["id_tournoi"]."
        AND `numRound` = ".$roundEnCours."
    ;";
    $req = $connection->prepare($strSql);
    if(!($req->execute())){
        die('Erreur SQL:'.print_r($req->errorInfo(), true)." (".$strSql.")");
    }
    $req->closeCursor();
} 

//--------------------------------------------------------------------------
//PAUSE
if($arrayValeur["action"] == "stop"){
    $strSql = "UPDATE `".$prefixTable."tab_tournois`
        SET `etatDecompte` = 'stop',
        `dateFin` = NOW()
        WHERE 1=1
        AND `id` = ".$arrayValeur["id_tournoi"]."
    ;";
    $req = $connection->prepare($strSql);
    if(!($req->execute())){
        die('Erreur SQL:'.print_r($req->errorInfo(), true)." (".$strSql.")");
    }
    $req->closeCursor(); 
}

//--------------------------------------------------------------------------
//REINIT
if($arrayValeur["action"] == "reinit"){
    $strSql = "UPDATE `".$prefixTable."tab_tournois`
        SET `etatDecompte` = 'init',
        `dateDebut` = '0000-00-00 00:00:00',
        `dateFin` = '0000-00-00 00:00:00',
        `roundEnCours` = 1
        WHERE 1=1
        AND `id` = ".$arrayValeur["id_tournoi"]."
    ;
    
    UPDATE `".$prefixTable."tab_rounds`
        SET 
        `debutDecompte` = '0000-00-00 00:00:00',
        `tempsRestantSeconde` = temps*60
        WHERE 1=1
        AND `id_tournoi` = ".$arrayValeur["id_tournoi"]."
    ;";
    $req = $connection->prepare($strSql);
    if(!($req->execute())){
        die('Erreur SQL:'.print_r($req->errorInfo(), true)." (".$strSql.")");
    }
    $req->closeCursor(); 
}

//--------------------------------------------------------------------------
//upRound
if($arrayValeur["action"] == "upRound"){
    $strSql = "SELECT COUNT(*) as 'exist'
        FROM `".$prefixTable."tab_rounds` a
        WHERE 1=1
        AND a.`id_tournoi` = ".$arrayValeur["id_tournoi"]."
        AND a.`numRound` = ".(intval($roundEnCours)+1)."
    ;";
    $req = $connection->prepare($strSql);

    $rows = array();
    if($req->execute()){
        $rows = $req->fetch();
        $exist = $rows["exist"];
    }else{
        die('Erreur SQL:'.print_r($req->errorInfo(), true)." (".$strSql.")");
    }
    $req->closeCursor();

    if($exist == "1"){
        $strSql = "UPDATE `".$prefixTable."tab_tournois`
            SET `roundEnCours` = ".(intval($roundEnCours)+1)."
            WHERE 1=1
            AND `id` = ".$arrayValeur["id_tournoi"]."
        ;
        
        UPDATE `".$prefixTable."tab_rounds`
            SET `tempsRestantSeconde` = 0
            WHERE 1=1
            AND `id_tournoi` = ".$arrayValeur["id_tournoi"]."
            AND `numRound` = ".$roundEnCours."
        ;
        
        UPDATE `".$prefixTable."tab_rounds`
            SET `debutDecompte` = NOW(),
            `tempsRestantSeconde` = temps*60
            WHERE 1=1
            AND `id_tournoi` = ".$arrayValeur["id_tournoi"]."
            AND `numRound` = ".(intval($roundEnCours)+1)."
        ;";
        $req = $connection->prepare($strSql);
        if(!($req->execute())){
            die('Erreur SQL:'.print_r($req->errorInfo(), true)." (".$strSql.")");
        }
        $req->closeCursor();   
    }else{
        $strSql = "UPDATE `".$prefixTable."tab_tournois`
            SET `etatDecompte` = 'stop',
            `dateFin` = NOW()
            WHERE 1=1
            AND `id` = ".$arrayValeur["id_tournoi"]."
        ;";
        $req = $connection->prepare($strSql);
        if(!($req->execute())){
            die('Erreur SQL:'.print_r($req->errorInfo(), true)." (".$strSql.")");
        }
        $req->closeCursor();
    }
}

//--------------------------------------------------------------------------

if($modeDebug){
    $strSorti .= ('<br><br><br><br>'.$strSql);
}

require("../API/php/footer.php");
?>